package com.zhuhjay.app.web.runner;

import com.zhuhjay.common.redis.util.RedisUtils;
import com.zhuhjay.service.system.model.entity.SystemBlack;
import com.zhuhjay.service.system.service.SystemBlackService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.zhuhjay.common.base.constant.CacheConstant.BLACK_KEY;

/**
 * 数据预热/初始化
 *
 * @author ZhuHJay
 * @date 2023/6/17 14:09
 */
@Component
public class DataInitializerRunner implements ApplicationRunner {

    @Resource
    private SystemBlackService blackService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        initBlackList();
    }

    /**
     * 初始化黑名单数据到缓存中
     */
    private void initBlackList() {
        // 将黑名单同步到缓存中
        RedisUtils.deleteCache(BLACK_KEY);
        String[] blackSet = blackService.lambdaQuery().list().stream().map(SystemBlack::getTarget).toArray(String[]::new);
        if (blackSet.length > 0) {
            RedisUtils.addValue2Set(BLACK_KEY, blackSet);
        }
    }

}
